 package com.pansky.integration.common.web;
 
 import com.pansky.integration.common.utils.BaiduAgentUtils;
 import com.pansky.integration.common.utils.CacheUtils;
 import com.pansky.integration.common.utils.HttpUtils;
 import java.io.IOException;
 import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
 import java.util.Set;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
 
 @Controller
 @RequestMapping({"/baidu"})
 public class BaiduMapAgentController
 {
   private HashMap<String, String> getHm(HttpServletRequest request)
   {
     String agentUtrl = (request.getRequestURL().toString().replace(request.getRequestURI(), "") + request.getContextPath()).replace("http://", "");
     agentUtrl = agentUtrl + "/baidu/agent";
 
     HashMap hm = new HashMap();
     hm.put("online4.map.bdimg.com", agentUtrl + "/online/4");
     hm.put("online3.map.bdimg.com", agentUtrl + "/online/3");
     hm.put("online2.map.bdimg.com", agentUtrl + "/online/2");
     hm.put("online1.map.bdimg.com", agentUtrl + "/online/1");
     hm.put("online0.map.bdimg.com", agentUtrl + "/online/0");
     hm.put("api.map.bdimg.com", agentUtrl + "/api");
     hm.put("api0.map.bdimg.com", agentUtrl + "/api/0");
     hm.put("api1.map.bdimg.com", agentUtrl + "/api/1");
     hm.put("api2.map.bdimg.com", agentUtrl + "/api/2");
     hm.put("api.map.baidu.com", agentUtrl + "/baidu");
 
     return hm;
   }
   @RequestMapping({"agent/baidu"})
   public void apiBaidu(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
     String url = "http://api.map.baidu.com/";
     String resultJs = HttpUtils.sendGet(url, request.getQueryString());
     response.setCharacterEncoding("UTF-8");
     PrintWriter out = response.getWriter();
     out.print(resultJs);
   }
   @RequestMapping({"agent/baidu/{a}/{b}"})
   public void apiBaidu(@PathVariable("a") String a, @PathVariable("b") String b, HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
     String servletPath = request.getServletPath();
     String url = "http://api.map.baidu.com/" + a + "/" + b + BaiduAgentUtils.getImageSuffix(servletPath);
     if (a.equals("images")||a.equals("customimage")) {
       HttpUtils.getImage(url, request, response);
     } else {
       String resultJs = HttpUtils.sendGet(url, request.getQueryString());
       response.setCharacterEncoding("UTF-8");
       PrintWriter out = response.getWriter();
       out.print(resultJs);
     }
   }
 
   @RequestMapping({"agent/api"})
   public void api(HttpServletRequest request, HttpServletResponse response, Model model)
     throws IOException
   {
     String url = "http://api.map.bdimg.com/";
     String resultJs = HttpUtils.sendGet(url, request.getQueryString());
     response.setCharacterEncoding("UTF-8");
     PrintWriter out = response.getWriter();
     out.print(resultJs);
   }
 
   @RequestMapping({"agent/api/{serverType}/{a}"})
   public void api(@PathVariable("serverType") String serverType, @PathVariable("a") String a, HttpServletRequest request, HttpServletResponse response, Model model)
     throws IOException
   {
     String servletPath = request.getServletPath();
     String url = "http://api" + serverType + ".map.bdimg.com/" + a + BaiduAgentUtils.getImageSuffix(servletPath);
     if (a.equals("images")||a.equals("customimage")) {
       HttpUtils.getImage(url, request, response);
     } else {
       String resultJs = HttpUtils.sendGet(url, request.getQueryString());
       response.setCharacterEncoding("UTF-8");
       PrintWriter out = response.getWriter();
       out.print(resultJs);
     }
   }
 
   @RequestMapping({"agent/api/{serverType}/{a}/{b}"})
   public void api(@PathVariable("serverType") String serverType, @PathVariable("a") String a, @PathVariable("b") String b, HttpServletRequest request, HttpServletResponse response, Model model)
     throws IOException
   {
     String servletPath = request.getServletPath();
     String url = "http://api" + serverType + ".map.bdimg.com/" + a + "/" + b + BaiduAgentUtils.getImageSuffix(servletPath);
     if (a.equals("images")||a.equals("customimage")) {
       HttpUtils.getImage(url, request, response);
     } else {
       String resultJs = HttpUtils.sendGet(url, request.getQueryString());
       response.setCharacterEncoding("UTF-8");
       PrintWriter out = response.getWriter();
       out.print(resultJs);
     }
   }
 
   @RequestMapping({"agent/online/{serverType}/{ywlx}"})
   public void online(@PathVariable("serverType") String serverType, @PathVariable("ywlx") String ywlx, HttpServletRequest request, HttpServletResponse response, Model model)
     throws IOException
   {
     if (StringUtils.isNotBlank(ywlx)) {
       String url = "http://online" + serverType + ".map.bdimg.com/" + ywlx + "/";
       if (ywlx.equals("js")) {
         String resultJs = HttpUtils.sendGet(url, request.getQueryString());
         response.setCharacterEncoding("UTF-8");
         PrintWriter out = response.getWriter();
         out.print(resultJs);
       } else if (ywlx.equals("tile")) {
         HttpUtils.getImage(url, request, response); } 
     }
   }
 
   @RequestMapping({"getscript"})
   @ResponseBody
   public String getscript(HttpServletRequest request, HttpServletResponse response, Model model) {
	   String cache = request.getParameter("cache");
     if ((StringUtils.isNotBlank(cache)) && (CacheUtils.get("script") != null) && (((String)CacheUtils.get("script")).length() > 2)) {
       return (String)CacheUtils.get("script");
     }
 
     String url = request.getParameter("agent");
     String content = HttpUtils.sendGet(url, "");
     Object[] rs = getHm(request).keySet().toArray();
     for (Object s : rs) {
       content = content.replaceAll((String)s, (String)getHm(request).get(s));
     }
//     CacheUtils.put("script", content);
     return content; }
   
   @RequestMapping({"getweather"})
   @ResponseBody
   public String getweather(HttpServletRequest request, HttpServletResponse response, Model model) throws UnsupportedEncodingException {
	   
     String url = new String(request.getParameter("agent").getBytes("ISO-8859-1"),"UTF-8");
     //StringBuffer domain = request.getRequestURL(); 
     //http://192.168.1.93:10080/DMC-IDX-AGENT/baidu/getweather
     //Access-Control-Allow-Origin:* 表示允许任何域名跨域访问
     //如果需要指定某域名才允许跨域访问，只需把Access-Control-Allow-Origin:*改为Access-Control-Allow-Origin:允许的域名
     //例如：header('Access-Control-Allow-Origin:http://www.client.com');     
     response.addHeader("Access-Control-Allow-Origin", "*");
     
     String content="";
	 try {
		content = HttpUtils.sendGet(url, "");
		
	 } catch (Exception e) {
		e.printStackTrace();// TODO: handle exception
	 }
     return content; }
   
   
   
   @RequestMapping({"refreshScript"})
   @ResponseBody
   public String refreshScript(HttpServletRequest request, HttpServletResponse response, Model model) {
//     CacheUtils.put("script", null);
     return "success";
   }
 }




